﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Dt261.Model;
using SQLite;

namespace Dt261.db
{
    class UserDb
    {
        private readonly SQLiteConnection connection;

        public UserDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            connection.CreateTable<User>();
        }



        public void AddUser(User user)
        {
            if (string.IsNullOrWhiteSpace(user.Uid) || string.IsNullOrWhiteSpace(user.Name))
                throw new Exception(" name and uid are required!");

            connection.Insert(new User()
            {
                CreateTime = DateTime.Now,
                DeviceID = user.DeviceID,
             
                
                IsGirl = user.IsGirl,
                IsLogin = true,//user.IsLogin,
                LastLoginTime = DateTime.Now,
                Name = user.Name,
                Password = user.Password,
                Phone = user.Phone,
                Uid = user.Uid,
                IsUpload = false

            });
        }

        public User LastUser()
        {
           IList < User > list = connection.Query < User >("select  * from user where IsLogin =?", true);

            return list.FirstOrDefault<User>();
        }

        public void UpdateUser(User user)
        {
            if (string.IsNullOrWhiteSpace(user.Uid) || string.IsNullOrWhiteSpace(user.Name))
                throw new Exception(" name and uid are required!");

            IList<User> list = connection.Query<User>("select * from user where user.uid = ?", user.Uid);

            if (list.Count() > 0)
            {
                connection.Update(user);
            }
            else
            {
                this.AddUser(user);
            }
        }

        public IList<User> GetUsers()
            => connection.Table<User>().ToList();

        public IList<User> GetUserByUid(string uid)
        {
            IList<User> list = connection.Query<User>("select * from user where user.uid = ?", uid);

            return list;
        }

        public IList<User> GetUserById(int id)
        {
            IList<User> list = connection.Query<User>("select * from user where user.id = ?", id);

            return list;
        }

        public IList<User> GetUserByName(string name)
        {
            IList<User> list = connection.Query<User>("select * from user where user.name = ?", name);

            return list;
        }

        public User GetOneUserByUid(string uid)
        {
            IList<User> list = GetUserByUid(uid);

            return list.Count()>0?list[0]:null;
        }


        public User GetOneUserByName(string name)
        {
            IList<User> list = GetUserByName(name);

            return list.Count() > 0 ? list[0] : null;
        }

        public User GetOneUserById(int id)
        {
            IList<User> list = GetUserById(id);

            return list.Count() > 0 ? list[0] : null;
        }

        public int DeleteUser(User user)
        {
            return connection.Delete(user);
        }

    }

}
